// 双向列表item类
class DoubleLinkedObj {
    data: any;
    next: null;
    prev: null;
    constructor(data: any) {
        this.data = data;
    }
}
export default class DoubleLinkedList {
    head: any = null;
    tail: any = null;
    length: number = 0;
    // 将元素加入链表
    public append(data: any): boolean {
        let obj = new DoubleLinkedObj(data);
        if (this.head === null) {
            this.head = obj;
        } else {
            let current = this.head;
            while (current.next) {
                current = current.next;
            }
            current.next = obj;
            obj.prev = current;
        }
        this.length += 1;
        return true;
    }
    // 将元素插入链表具体位置
    public insert(position: number, data: any): boolean {
        if (position < 0 || position > this.length) return false;
        if (position === this.length) {
            return this.append(data);
        }
        let newNode = new DoubleLinkedObj(data);
        if (position == 0) {
            newNode.next = this.head;
            this.head = newNode;
        } else {
            var prev = null;
            var current = this.head;
            let index = 0;
            while (index++ < position) {
                prev = current;
                current = current.next;
            }
            newNode.next = current;
            current.prev = newNode;
            prev.next = newNode;
            newNode.prev = prev;
        }
        this.length += 1;
        return true;
    }
    // 获取具体位置的链表内容
    public get(position: number) {
        if (position < 0 || position > this.length) return void 0;
        if (this.head === null) return null;
        var current = this.head;
        let index = 0;
        while (index++ < position) {
            current = current.next;
        }
        return current.data;
    }
    // 查找具体内容的链表位置
    public indexOf(data: any) {
        var current = this.head;
        let index = 0;
        while (current && current.data !== data) {
            current = current.next;
            index++;
        }
        if (index >= this.length) {
            return -1;
        } else {
            return index;
        }
    }
    // 更新链表内容
    public update(position: number, data: any): boolean {
        if (position < 0 || position > this.length) return false;
        var current = this.head;
        let index = 0;
        while (index++ < position) {
            current = current.next;
        }
        if (current) {
            current.data = data;
        } else {
            return false;
        }
        return true;
    }
    // 删除链表具体的位置的项
    public removeAt(position: number): boolean {
        if (position < 0 || position > this.length) return false;
        var prev = null;
        var current = this.head;
        let index = 0;
        while (index++ < position) {
            prev = current;
            current = current.next;
        }
        if (current) {
            prev.next = current.next;
            current.next.prev = prev;
            current = null;
        } else {
            return false;
        }
        this.length -= 1;
        return true;
    }
    //
    public remove(data: any): boolean {
        var prev = null;
        var current = this.head;
        while (current && current.data !== data) {
            prev = current;
            current = current.next;
        }
        if (current) {
            prev.next = current.next;
            current.next.prev = prev;
            current = null;
        } else {
            return false;
        }
        this.length -= 1;
        return true;
    }
    // 判断链表是否为空
    public isEmpty(): boolean {
        return !this.length;
    }
    // 链表中元素的个数
    public size(): number {
        return this.length;
    }
    // toString 12 235 25
    public toString(): string {
        let current = this.head;
        let arr = [];
        while (current) {
            arr.push(current.data);
            current = current.next;
        }
        return arr.join(" ");
    }
    //  正向返回数据
    public forwardString(): string {
        return this.toString();
    }
    // 反向返回数据
    public backwardString(): string {
        let current = this.tail;
        let arr = [];
        while (current) {
            arr.push(current.data);
            current = current.prev;
        }
        return arr.join(" ");
    }
}
